<?php
class model_formulalist extends framework_base_model {
    /**
     * 获取大分类列表
     * @param number $nextNum
     * @param number $pagesize
     * @param unknown $condition
     * @return unknown|Ambigous <number, unknown>
     */
    public static function get_formulalist_list($nextNum = 0,$pagesize = 0,$condition = array()) {
		$filed = "a.`product_id`,a.`produce_name`,a.`parent_id`,a.`area_motor_id`,a.`area_smotor_id`,a.`area_ship_id`,a.`area_train_id`,a.`raw_add_time`,a.`raw_update_time`,b.`product_class_id`,b.`class_name`,a.`status`,a.`author`,a.`editer`,c.`formula_id`";
    	$sql = "select {$filed} from weixin_product a left join weixin_product_class b on a.`parent_id`=b.`product_class_id` left join `weixin_formula` as c on a.`product_id`=c.`small_class_id` and c.`delete_flag` = 0 where a.`delete_flag` = 0 ";
		$where = "";
    	if(is_array($condition)&& count($condition)>0){
    		if(isset($condition['class_big_name'])) {
    			$where .= " and b.`class_name` like '%{$condition['class_big_name']}%'";
    		}
			if(isset($condition['class_small_name'])) {
    			$where .= " and a.`produce_name` like '%{$condition['class_small_name']}%'";
    		}
    	}
    	$where .= " order by a.`product_id` desc";
    	$limit = "";
        if($pagesize != 0 ){
    		$limit = " limit {$nextNum},{$pagesize}";
    	}
    	$result = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql.$where.$limit);
    		$st->execute();
    		$result = $st->fetchAll();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result = array();
    	}
		
		$sql = "select count(a.`product_id`) from weixin_product a left join weixin_product_class b on a.`parent_id`=b.`product_class_id` left join `weixin_formula` as c on a.`product_id`=c.`small_class_id` and c.`delete_flag` = 0 where a.`delete_flag` = 0 {$where}";
		$binds2 = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute($binds2);
    		$count = $st->fetchColumn();
    		$result['count'] = $count;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result['count'] = 0;
    	}		
		return $result;
    }
	public static function get_produce_info($product_id) {
		$filed = "a.`product_id`,a.`produce_name`,a.`parent_id`,a.`area_motor_id`,a.`area_smotor_id`,a.`area_ship_id`,a.`area_train_id`,a.`raw_add_time`,a.`raw_update_time`,b.`product_class_id`,b.`class_name`,a.`status`,a.`author`,a.`editer`,c.`formula_id`";
    	$sql = "select {$filed} from weixin_product a left join weixin_product_class b on a.`parent_id`=b.`product_class_id` left join `weixin_formula` as c on a.`product_id`=c.`small_class_id` and c.`delete_flag` = 0 where a.`delete_flag` = 0 and a.`product_id`=:product_id limit 1";
    	$result = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute(array(':product_id'=>$product_id));
    		$result = $st->fetch();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}		
		return $result;
    }
}
